package com.kdd.campus.sh.controller;

import com.kdd.campus.common.entity.CommonResult;
import com.kdd.campus.common.util.RedisUtil;
import com.kdd.campus.sh.convert.UserConvert;
import com.kdd.campus.sh.dto.UserDto;
import com.kdd.campus.sh.po.ShFootprint;
import com.kdd.campus.sh.po.User;
import com.kdd.campus.sh.service.UserService;
import com.kdd.campus.sh.util.JwtTokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/****
 * @Author:shenkunlin
 * @Description:
 * @Date 2019/6/14 0:18
 *****/
@Api(value = "用户管理")
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController extends BaseController {


    @Autowired
    private UserService userService;

    @Autowired
    public JwtTokenUtil jwtTokenUtil;

    @ApiOperation(value = "获取用户信息")
    @GetMapping
    public CommonResult getUserInfo(){
        Long userId = Integer.toUnsignedLong((Integer) RedisUtil.get(jwtTokenUtil.getToken(request)));
        User user = userService.findById(userId);
        UserDto userDto = UserConvert.INSTANCE.poToDto(user);
        return CommonResult.success(userDto);
    }

    @ApiOperation(value = "修改用户信息")
    @PutMapping
    public CommonResult updateUser(@RequestBody UserDto userDto){
        Long userId = Integer.toUnsignedLong((Integer) RedisUtil.get(jwtTokenUtil.getToken(request)));
        User user = UserConvert.INSTANCE.dtoToPo(userDto);
        user.setId(userId);

        userService.update(user);
        return CommonResult.success();
    }
    @ApiOperation(value = "增加用户")
    @PostMapping
    public CommonResult saveUser(@RequestBody UserDto userDto){
        User user = UserConvert.INSTANCE.dtoToPo(userDto);
        userService.save(user);
        return CommonResult.success();
    }
    @ApiOperation(value = "删除用户")
    @GetMapping
    public CommonResult delUser(){
        Long userId = Integer.toUnsignedLong((Integer) RedisUtil.get(jwtTokenUtil.getToken(request)));
        userService.delete(userId);
        return CommonResult.success();
    }

    @ApiOperation(value = "我的足迹")
    @GetMapping("/footprint")
    public CommonResult getFootprint(){
        Long userId = Integer.toUnsignedLong((Integer) RedisUtil.get(jwtTokenUtil.getToken(request)));
        List<ShFootprint> footprintList = userService.getFootprint(userId);

        return CommonResult.success(footprintList);
    }


}
